#include "ThreadPool.h"
// #include "NewThreadPool.h"

int squre(int i)
{
    std::cout << "Hello from task " << i << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Task " << i << " completed" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return i * i;
}

int main()
{
    std::cout << "this main thread id is " << std::this_thread::get_id() << std::endl;
    // Create a thread pool with 4 worker threads.
    ThreadPool pool(4);

    // Create a thread pool with 4 worker threads, max 10 worker threads.
    // ThreadPool pool(4, 10);

    // Enqueue some tasks
    std::vector<std::future<int>> results;
    for (int i = 1; i < 21; i++)
    {
        // all is well
        // results.emplace_back(pool.enqueue(&squre, i));
        // results.emplace_back(pool.enqueue(squre, i));
        results.emplace_back(pool.enqueue([i]()
                                          {
                            std::cout << "Hello from task " << i << std::endl;
                            std::this_thread::sleep_for(std::chrono::seconds(1));
                            std::cout << "Task " << i << " completed" << std::endl;
                            std::this_thread::sleep_for(std::chrono::seconds(1));
                            return i * i ; }));
    }

    std::this_thread::sleep_for(std::chrono::seconds(3));

    // use get() to get future result.
    for (auto &result : results)
        printf("Task completed with result: %d\n", result.get());

    return 0;
}
